package com.mwf.domain.activity.service.trial.node;

import com.alibaba.fastjson.JSON;
import com.mwf.domain.activity.model.entity.MarketProductEntity;
import com.mwf.domain.activity.model.entity.TrialBalanceEntity;
import com.mwf.domain.activity.model.valobj.GroupBuyActivityDiscountVO;
import com.mwf.domain.activity.model.valobj.SkuVo;
import com.mwf.domain.activity.service.AbstractGroupBuyMarketSupport;
import com.mwf.domain.activity.service.discount.IDiscountCalculateService;
import com.mwf.domain.activity.service.trial.factory.DefaultActivityStrategyFactory;
import com.mwf.domain.activity.service.trial.thread.QueryGroupBuyActivityDiscountVOThreadTask;
import com.mwf.domain.activity.service.trial.thread.QuerySkuVOFromDBThreadTask;
import com.mwf.types.design.framework.tree.StrategyHandler;
import com.mwf.types.enums.ResponseCode;
import com.mwf.types.exception.AppException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Map;
import java.util.concurrent.FutureTask;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Slf4j
@Service
public class MarketNode extends AbstractGroupBuyMarketSupport<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> {

    @Resource
    private EndNode endNode;

    @Resource
    private Map<String, IDiscountCalculateService> discountCalculateServiceMap;

    @Override
    protected TrialBalanceEntity doApply(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        log.info("拼团商品查询试算服务-MarketNode userId:{} requestParameter:{}", requestParameter.getUserId(), JSON.toJSONString(requestParameter));

        //拼团优惠试算
        GroupBuyActivityDiscountVO.GroupBuyDiscount groupBuyDiscount = dynamicContext.getGroupBuyActivityDiscountVO().getGroupBuyDiscount();
        SkuVo skuVo = dynamicContext.getSkuVo();
        if (null == groupBuyDiscount || null == skuVo) {
            return router(requestParameter, dynamicContext);
        }
        String marketPlan = groupBuyDiscount.getMarketPlan();
        IDiscountCalculateService discountCalculateService = discountCalculateServiceMap.get(marketPlan);
        if (discountCalculateService == null) {
            log.info("不存在{}类型的折扣计算服务，支持类型为:{}", groupBuyDiscount.getMarketPlan(), JSON.toJSONString(discountCalculateServiceMap.keySet()));
            throw new AppException(ResponseCode.E0001.getCode(), ResponseCode.E0001.getInfo());
        }

        //折扣价格
        BigDecimal deductionPrice = discountCalculateService.calculate(requestParameter.getUserId(), skuVo.getOriginalPrice(), groupBuyDiscount);
        dynamicContext.setDeductionPrice(deductionPrice);

        return router(requestParameter, dynamicContext);
    }

    @Override
    public StrategyHandler<MarketProductEntity, DefaultActivityStrategyFactory.DynamicContext, TrialBalanceEntity> get(MarketProductEntity requestParameter, DefaultActivityStrategyFactory.DynamicContext dynamicContext) throws Exception {
        return endNode;
    }
}
